package com.abeir.bilibili.convert.deprecated;

import com.abeir.bilibili.convert.Scanner;

import java.io.File;
import java.nio.file.Path;
import java.util.Optional;

/**
 * 通过指定的后缀名来获取视频或音频文件
 *
 * @deprecated 不推荐使用，应该使用 VideoScanner 或 AudioScanner
 * @see com.abeir.bilibili.convert.VideoScanner
 * @see com.abeir.bilibili.convert.AudioScanner
 */
public class VideoAudioScanner implements Scanner {

    private final String[] suffixes;

    public VideoAudioScanner(String[] suffixes){
        this.suffixes = suffixes;
    }

    @Override
    public Optional<File> target(Path dir) {
        File[] files = dir.toFile().listFiles((d, name) -> {
            boolean found = false;
            for(String suffix : suffixes){
                found = name.endsWith(suffix);
                if(found)
                    break;
            }
            return found;
        });
        if(files == null || files.length == 0)
            return Optional.empty();
        if(files.length > 1){
            throw new IllegalArgumentException("Multiple video files were discovered.");
        }
        return Optional.of(files[0]);
    }
}
